🎵 Jingle Bell Button Game

We created this activity using ChatGPT, an AI tool. We've not had time to test it, so it might not work correctly. If it doesn't quite work, try to fix it if you can!

Welcome! In this project, you’ll build a fun Christmas guessing game using your Raspberry Pi Pico. When you press the button, a random Christmas tune will play, and everyone can guess which tune it is! 🎄

Step 1: Hardware Setup

Let’s start by connecting all the components to your Raspberry Pi Pico. We’ll use jumper wires to directly connect the components.

1. Components You’ll Need

2. Connecting the Components

Connect the Buzzer:

Connect the Push Button:

3. Powering the Circuit

Once all connections are in place, plug your Raspberry Pi Pico into your computer using a USB cable. This will power the Pico and the components.

🎉 Great job! You’ve wired up your buzzer and button correctly. Now we’re ready to code and bring the project to life!

Step 2: Writing the Code

Now we’ll program the Raspberry Pi Pico to play random Christmas tunes when the button is pressed. Follow the step-by-step instructions carefully, and don’t forget to test your project as you go!

1. Import Libraries

Type the following code to import the libraries needed for controlling the GPIO pins and generating random tunes:

import machine import utime import random

What does this do?

✨ Nice start! You’ve imported everything needed to work with the Pico and play random tunes.

2. Set Up the Pins

Next, set up the GPIO pins for the buzzer and button:

buzzer = machine.Pin(15, machine.Pin.OUT) button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)

What does this do?

🎉 Excellent! Your Pico now knows which pins are connected to the button and buzzer.

3. Define the Core Christmas Tunes

We’ll store Christmas tunes in a **dictionary**, a type of array that uses a key-value pair. Each key is the tune’s name, and the value is a list of notes and durations. Here’s how:

melodies = { "Jingle Bells": [(440, 0.5), (440, 0.5), (440, 1)], "Silent Night": [(330, 0.8), (294, 0.8), (262, 1.2)] }

Explanation:

🎶 Fantastic! You’ve set up your first tunes. Let’s write code to play them!

Step 3: Write the Function to Play a Tune

Write this function to play a melody:

def play_tune(melody): for note, duration in melody: buzzer.freq(note) buzzer.duty_u16(1000) utime.sleep(duration) buzzer.duty_u16(0) utime.sleep(0.1)

Explanation:

🎵 Great! You’re ready to use the button to play a random tune!

Step 4: Randomly Play a Tune

Now let’s make the button play a random tune when pressed:

while True: if button.value() == 1: tune_name = random.choice(list(melodies.keys())) print("Playing:", tune_name) play_tune(melodies[tune_name]) utime.sleep(1)

Explanation:

🎉 Awesome! You can now play random tunes when you press the button!

Step 5: Add More Christmas Tunes

Want to expand your project? Add these tunes to your dictionary:

melodies["Deck the Halls"] = [(392, 0.4), (440, 0.4), (392, 0.4), (349, 0.4)] melodies["We Wish You a Merry Christmas"] = [(262, 0.4), (294, 0.4), (330, 0.4), (262, 0.4)]

Explanation:

🎶 Amazing work! Keep adding tunes and make your project even more festive!